package com.luoxiang.socialandroid.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.bean.Label;

import java.util.List;

public class Tezhi4RecyAdapter extends RecyclerView.Adapter<Tezhi4RecyAdapter.ViewHolder>{
    private List<Label> list;
    private String url = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/";
    public Tezhi4RecyAdapter(List<Label> list) {
        this.list = list;
    }


    public static interface OnAddClickListener {
        void onItemClicks(int position,int id,boolean isselect); //传递boolean类型数据给activity
    }

    OnAddClickListener onItemAddClick;

    public void setOnAddClickListener(OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }
    @NonNull
    @Override
    public Tezhi4RecyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_tezhi4, parent, false);
        Tezhi4RecyAdapter.ViewHolder holder = new Tezhi4RecyAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull Tezhi4RecyAdapter.ViewHolder holder, @SuppressLint("RecyclerView")int position) {
        Label bean = list.get(position);
        Glide.with(MyAppliaction.getContext())
                .load(url + bean.getImagePath())
                .transform(new CenterCrop(),new RoundedCorners(37))
                .into(holder.im_tezhi4);
        holder.tx1_tezhi4.setText(bean.getTitle());
        holder.tx2_tezhi4.setText(bean.getContent());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                多选
                bean.setSelect(!bean.isSelect());
                notifyDataSetChanged();
//                单选放开
//                for (Label item : list) {
//                    if (item.getId() == bean.getId()) {
//                        item.setSelect(!item.isSelect());
//                    } else {
//                        item.setSelect(false);
//                    }
//                    notifyDataSetChanged();
//                }
            }

        });
        if (bean.isSelect()) {
            holder.gouxuan1.setVisibility(View.VISIBLE);
            holder.imgBg.setVisibility(View.VISIBLE);
        } else {
            holder.gouxuan1.setVisibility(View.INVISIBLE);
            holder.imgBg.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean selected = bean.isSelect();
                onItemAddClick.onItemClicks(position,list.get(position).getId(),!bean.isSelect());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView im_tezhi4;
        TextView tx1_tezhi4;
        TextView tx2_tezhi4;
        ImageView gouxuan1;
        View imgBg;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            im_tezhi4 = itemView.findViewById(R.id.im_tezhi4);
            tx1_tezhi4 = itemView.findViewById(R.id.tx1_tezhi4);
            tx2_tezhi4 = itemView.findViewById(R.id.tx2_tezhi4);
            gouxuan1 = itemView.findViewById(R.id.gouxuan1);
            imgBg = itemView.findViewById(R.id.img_bg);
        }
    }
}
